//
// Created by dong on 2025/10/21.
//

#include "ColorBar.h"

ColorBar::ColorBar()
{
    SDL_Color color[20]={
        {0,0,0},
        {127,127,127},
        {136,0,21},
        {237,28,36},
        {255,127,39},
        {255,242,0},
        {34,177,76},
        {0,162,132},
        {63,72,204},
        {163,73,164},
        {255,255,255},
        {195,195,195},
        {185,122,87},
        {255,174,201},
        {255,201,14},
        {239,228,176},
        {181,230,29},
        {153,217,234},
        {112,146,190},
        {200,191,231},
    };

    SDL_Rect dst ={WINDOW_WIDTH*3/5,10,15,15};
    for (int i=0;i<30;i++)
    {
        if (i<10)
        {
            SDL_Rect r1= {dst.x+ dst.w * i + i*6,dst.y,dst.w,dst.h};
            m_colorBar.push_back(Button(r1,"",color[i]));
        }else if (i<20)
        {
            int pos = i-10;
            SDL_Rect r2= {dst.x+ dst.w * pos + pos*6,dst.y+dst.h+6,dst.w,dst.h};
            m_colorBar.push_back(Button(r2,"",color[i]));
        }else
        {
            int pos = i-20;
            SDL_Rect r3= {dst.x+ dst.w * pos + pos*6,dst.y+(dst.h+6)*2,dst.w,dst.h};
            m_colorBar.push_back(Button(r3,"",{245,246,247,0}));
        }
    }
    isClicked=0;
    SDL_Rect r4 = {dst.x-90,4,40,66};
    colorView = new Button(r4,"",{0,0,0,0});
}

bool ColorBar::mousePressEvent(SDL_Event* msg)
{

    for (int i=0;i<m_colorBar.size();i++)
    {
        if (m_colorBar[i].mousePressEvent(msg)==true)
        {
            isClicked=i;
            return true;
        }

    }
    if (colorView->mousePressEvent(msg))
    {
        return true;
    }


    return  false;
}

void ColorBar::render(SDL_Renderer* render)
{
    for (auto v:m_colorBar)
    {
        v.render(render,Button::ColorTool);
    }
    colorView->setColor(color());
    colorView->render(render,Button::Menu);
}

SDL_Color ColorBar::color()
{
    return m_colorBar[isClicked].getColor();
}
